package com.hrms.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

public class ListSerializer {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static String serialize(Object object) throws JsonProcessingException {
        return objectMapper.writeValueAsString(object);
    }

    public static <T> T deserialize(String jsonString, Class<T> valueType) throws JsonProcessingException {
        return objectMapper.readValue(jsonString, valueType);
    }

    public static String serializeList(List<?> list) {
        try {
            return objectMapper.writeValueAsString(list);
        } catch (Exception e) {
            throw new RuntimeException("Error serializing list to string", e);
        }
    }

    public static List<?> deserializeList(String jsonString) {
        try {
            return objectMapper.readValue(jsonString, List.class);
        } catch (Exception e) {
            throw new RuntimeException("Error deserializing string to list", e);
        }
    }
}